package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) {
        return orderService.findPage(pageBean);
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Map map, Integer[] setmealIds) {
        try {
            map.put("orderType", Order.ORDERTYPE_TELEPHONE);
            map.put("orderStatus", Order.ORDERSTATUS_NO);
            return orderService.add(map, setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    //根据id查询检查组
    @RequestMapping("/findByOrderId")
    public Result findByMealIdAndSetmealId(Integer id){
        try{
            Map resultMap = orderService.findByOrderId(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,resultMap);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }



    @RequestMapping("/findSetmealByOrderId")
    public Result findSetmealByOrderId(Integer id) {
        try {
            List<Integer> setmealIds = orderService.findSetmealByOrderId(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //根据id删除检查项
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            orderService.delete(id);
            return new Result(true, MessageConstant.DELETE_ORDER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDER_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map map,Integer id){
        try{
            orderService.edit(map,id);
            return new Result(true, MessageConstant.EDIT_ORDER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ORDER_FAIL);
        }
    }

    //编辑
    @RequestMapping("/editOrderStatus")
    public Result editOrderStatus(Integer id){
        try{
            Integer result = orderService.editOrderStatus(id);
            return new Result(true, MessageConstant.EDIT_ORDER_SUCCESS,result);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ORDER_FAIL);
        }
    }

}
